/* 
 *
 * Created by Mallik Anakti 11/23/2004
 * 
 */

package com.scalar.chiptrack.sales.actions;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.commons.Labels;
import com.scalar.chiptrack.commons.SessionKeys;
import com.scalar.chiptrack.sales.Customer;
import com.scalar.chiptrack.sales.dao.CustomerDAO;
import com.scalar.chiptrack.sales.dao.CustomerRelationshipDAO;

public class CustomerRelationshipEditAction extends ChipTrackAction{
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, 
    						HttpServletResponse response) throws Exception {
    	
    	String customerId = request.getParameter("customerId");
    	String action = request.getParameter("action");
		
    	if("searchodms".equalsIgnoreCase(action)){
    		//System.out.println("This is CustomerRelationshipEditAction");
    		return getAllODMList(mapping, form, request, response);
    	}
    	if("saveodms".equalsIgnoreCase(action)){
    		//System.out.println("This is CustomerRelationshipEditAction");
    		return saveAllODMList(mapping, form, request, response);
    	}
    	if("searchendcustomers".equalsIgnoreCase(action)){
    		//System.out.println("This is CustomerRelationshipEditAction");
    		return getAllEndCustomerList(mapping, form, request, response);
    	}
    	if("saveendcustomers".equalsIgnoreCase(action)){
    		//System.out.println("This is CustomerRelationshipEditAction");
    		return saveAllEndcustomerList(mapping, form, request, response);
    	}
    	int custId = 0;
    	
    	try{
    		
    		custId = Integer.parseInt(customerId);
    		
    	}catch(NumberFormatException ignore){
    		
    	}
    	
    	Customer customer = CustomerDAO.loadCustomerById(custId);
    	
    	ArrayList parentChildList = CustomerRelationshipDAO.getParentChildRelationshipInformation(customerId);
    	ArrayList odmList = CustomerRelationshipDAO.getODMRelationshipInformation(customerId);
    	ArrayList endCustomerList = CustomerRelationshipDAO.getEndCustomerRelationshipInformation(customerId);
    	
    	request.setAttribute("parentChildList", parentChildList);
    	request.setAttribute("odmList", odmList);
    	request.setAttribute("endCustomerList", endCustomerList);
    	request.setAttribute("customerName", customer.getCustomerName());
    	request.setAttribute("customerId", customerId);
    	
    	return mapping.findForward("displayList");
    }
    
    public ActionForward getAllODMList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
    	
    	String customerName = request.getParameter("customerName");
    	String customerId = request.getParameter("customerId");
    	
    	ArrayList odmList = CustomerRelationshipDAO.getAllOdmList(customerId);
    	
    	request.setAttribute("odmList", odmList);
    	request.setAttribute("customerName", customerName);
    	request.setAttribute("customerId", customerId);
    	
    	return mapping.findForward("odmList");
    }
   
    public ActionForward saveAllODMList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
    	
    	String customerName = request.getParameter("customerName");
    	String customerId = request.getParameter("customerId");
    	
    	HashMap info = new HashMap();
    	HashMap errors = new HashMap();
    	
    	//System.out.println("CustomerId = "+customerId+"; Name = "+customerName);
    	String odmId[] = request.getParameterValues("odmId");
        String endCustomerLabel = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.sales.endCustomer" );

    	if(odmId == null){
    		
    		if(CustomerRelationshipDAO.deactivateOdmList(customerId)){
    			
    			info.put("Customer and "+endCustomerLabel+" Relation updated successfully", null);
    		}
    		
    		ArrayList odmList = CustomerRelationshipDAO.getAllOdmList(customerId);
    		
	    	request.setAttribute("odmList", odmList);
	    	request.setAttribute("customerName", customerName);
	    	request.setAttribute("customerId", customerId);
	    	request.setAttribute("Info", info);
    	
    		return mapping.findForward("odmList");
    	}
    	
	    /*for(int i=0; i<odmId.length; i++){
	    	System.out.println("odmId = "+odmId[i]);
	    }*/
	    
	    if(CustomerRelationshipDAO.deactivateOdmList(customerId)){
    			
    			//info.put(InfoMessages.CUSTOMER_RELATIONSHIP_UPDATE_ODM, null);
    	}
    	
    	if(CustomerRelationshipDAO.saveOdmList(customerId, odmId)){
    		
    		request.setAttribute("closeWindow", "true");
    		info.put("Customer and "+endCustomerLabel+" Relation Saved successfully", null);
    		
    	}
    	
    	ArrayList odmList = CustomerRelationshipDAO.getAllOdmList(customerId);
    	
    	request.setAttribute ( SessionKeys.ERROR_MESSAGES, errors );
    	request.setAttribute ( SessionKeys.INFO_MESSAGES, info );
    	
    	request.setAttribute("odmList", odmList);
    	request.setAttribute("customerName", customerName);
    	request.setAttribute("customerId", customerId);
    	request.setAttribute("Info", info);
    	
    	return mapping.findForward("odmList");
    }
   
   	public ActionForward getAllEndCustomerList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
    	
    	String customerName = request.getParameter("customerName");
    	String customerId = request.getParameter("customerId");
    	
    	ArrayList endCustomerList = CustomerRelationshipDAO.getAllEndCustomerList(customerId);
    	
    	request.setAttribute("endCustomerList", endCustomerList);
    	request.setAttribute("customerName", customerName);
    	request.setAttribute("customerId", customerId);
    	
    	return mapping.findForward("endCustomerList");
    }
    
    public ActionForward saveAllEndcustomerList(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception{
    	
    	String customerName = request.getParameter("customerName");
    	String customerId = request.getParameter("customerId");
    	
    	HashMap info = new HashMap();
    	HashMap errors = new HashMap();
    	
    	//System.out.println("CustomerId = "+customerId+"; Name = "+customerName);
    	String endcustomerId[] = request.getParameterValues("odmId");
        String endUserLabel = MessageResources.getMessageResources ( Labels.APPLICATION_RESOURCE_PROPERTIES ).getMessage ( "label.sales.endUser" );

    	if(endcustomerId == null){
    		
    		if(CustomerRelationshipDAO.deactivateEndCustomerList(customerId)){
    			
    			info.put("Customer and "+endUserLabel+" Saved successfully", null);
    		}
    		
    		ArrayList endCustomerList = CustomerRelationshipDAO.getAllEndCustomerList(customerId);
    	
	    	request.setAttribute("endCustomerList", endCustomerList);
	    	request.setAttribute("customerName", customerName);
	    	request.setAttribute("customerId", customerId);
	    	request.setAttribute("Info", info);
    	
    		return mapping.findForward("endCustomerList");
    	}
    	
	    /*for(int i=0; i<endcustomerId.length; i++){
	    	System.out.println("endcustomerId = "+endcustomerId[i]);
	    }*/
    	
    	if(CustomerRelationshipDAO.deactivateEndCustomerList(customerId)){
    			
    		//info.put(InfoMessages.CUSTOMER_RELATIONSHIP_UPDATE_ODM, null);
    	}
    		
    	if(CustomerRelationshipDAO.saveEndCustomerList(customerId, endcustomerId)){
    		
    		request.setAttribute("closeWindow", "true");
    		info.put("Customer and "+endUserLabel+" Relation Saved successfully", null);
    		
    	}
    	
    	ArrayList endCustomerList = CustomerRelationshipDAO.getAllEndCustomerList(customerId);
    	
    	request.setAttribute ( SessionKeys.ERROR_MESSAGES, errors );
    	request.setAttribute ( SessionKeys.INFO_MESSAGES, info );
    	
    	request.setAttribute("endCustomerList", endCustomerList);
    	request.setAttribute("customerName", customerName);
    	request.setAttribute("customerId", customerId);
    	request.setAttribute("Info", info);
    	
    	return mapping.findForward("endCustomerList");
    }
}